using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Musicman.Business;

namespace MusicmanWeb.USER
{
    public partial class EditProfile : MusicmanPage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            ReddirectNonActiveSession();
        }

        protected void Update_Button(object sender, CommandEventArgs e)
        {
            string pID = ((HiddenField)this.Repeater1.Controls[1].FindControl("PersonID")).Value;
            string firstName = ((TextBox)this.Repeater1.Controls[1].FindControl("Firstname")).Text;
            string lastName = ((TextBox)this.Repeater1.Controls[1].FindControl("Lastname")).Text;
            long personID = Int64.Parse(pID);
            Musicman.Entities.User user = DataManager.Instance.GetUserInfo(personID);

            bool resume = true;
            
            if (!String.IsNullOrEmpty(Request["changePasswordCb"]))
            {
                string oldPassword = ((TextBox)this.Repeater1.Controls[1].FindControl("OldPassword")).Text;
                string newPassword = ((TextBox)this.Repeater1.Controls[1].FindControl("Newpassword")).Text;
                string newPasswordRetype = ((TextBox)this.Repeater1.Controls[1].FindControl("Newpassword2")).Text;
                if (user.Password != DataManager.Instance.PassEncrypting(oldPassword))
                {
                    ((Label)this.Repeater1.Controls[1].FindControl("ValidationMessage")).Text = "Wrong old password";
                    resume = false;
                }
                else
                {
                    if (newPassword != newPasswordRetype || newPasswordRetype.Trim() == "")
                    {
                        ((Label)this.Repeater1.Controls[1].FindControl("ValidationMessage")).Text = "Retype password correctly";
                        resume = false;
                    }
                    else
                    {
                        user.Password = DataManager.Instance.PassEncrypting(newPasswordRetype);
                    }
                }
            }
            if (resume)
            {
                user.Firstname = firstName;
                user.Lastname = lastName;
                user.MailRequest = ((CheckBox)this.Repeater1.Controls[1].FindControl("MailRequestCheckBox")).Checked;
                DataManager.Instance.SaveUser(user);
                ((TextBox)this.Repeater1.Controls[1].FindControl("OldPassword")).Enabled = false;
                ((TextBox)this.Repeater1.Controls[1].FindControl("Newpassword")).Enabled = false;
                ((TextBox)this.Repeater1.Controls[1].FindControl("Newpassword2")).Enabled = false;
                this.Repeater1.DataBind();
                ((Label)this.Repeater1.Controls[1].FindControl("ValidationMessage")).Visible = false;                
            }
            else
            {
                ((Label)this.Repeater1.Controls[1].FindControl("ValidationMessage")).Visible = true;
                ((TextBox)this.Repeater1.Controls[1].FindControl("OldPassword")).Enabled = true;
                ((TextBox)this.Repeater1.Controls[1].FindControl("Newpassword")).Enabled = true;
                ((TextBox)this.Repeater1.Controls[1].FindControl("Newpassword2")).Enabled = true;
            }
        }
    }
}
